<?php
namespace services\models;

/**
 * Description of ProductSpec
 *
 * @author Administrator
 */
class ProductSpec extends \common\models\ProductSpec {
    public static function getDb() {
        return \Yii::$app->db;
    }

    /**
     * 通过SKU ID获取规格信息
     * @param int $product_id SKU ID
     * @return array SKU的规格信息
     */
    public static function findByProductId($product_id) {
        $vars = self::find()->where('product_id=:product_id', [':product_id' => $product_id])->all();
        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }

    /**
     * 通过商品ID获取规格信息
     * @param int $goods_id 商品ID
     * @return array 返回SKU的规格信息
     */
    public static function findByGoodsId($goods_id) {
        $vars = self::find()->where('goods_id=:goods_id', [':goods_id' => $goods_id])->all();
        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }

    /**
     * 通过商品ID，从数据库获取SKU规格信息
     * @param int $goods_id 商品ID
     * @return array
     */
    public static function findByGoodsIdFromDb($goods_id) {
        $vars = self::find()->where('goods_id=:goods_id', [':goods_id' => $goods_id])->all();
        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }

    /**
     * 通过商品ID和SKU ID获取规格信息
     * @param int $goods_id 商品ID
     * @param int $product_id SKU ID
     * @return array 规格信息
     */
    public static function findByGoodsIdAndProductId($goods_id, $product_id) {
        $params = [':goods_id' => $goods_id, ':product_id' => $product_id];
        $vars = self::find()->where('goods_id=:goods_id AND product_id=:product_id', $params)->all();
        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }

    /**
     * 通过product_spec_id获取一条SKU规格记录
     * @param int $product_spec_id product_spec表的自增长ID
     * @return array 规格信息
     */
    public static function findByProductSpecId($product_spec_id) {
        $var = self::find()->where('product_spec_id=:product_spec_id', [':product_spec_id' => $product_spec_id])->one();
        if(empty($var)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($var);
    }    
    /**
     * 获取当前商品所有规格ID
     * @param 商品ID $product_id
     * @return array
     */
    public static function findByProductIdByDefault($product_id) {
        $vars = self::find()
                ->leftJoin('d_try.product p','product_spec.product_id = p.product_id')
                ->where('p.product_id=:product_id AND p.sale_status = 1 AND p.status = 0', [':product_id' => $product_id])
                ->all();

        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }
    
    /**
     * 获取规格信息根据goods_id根据spec_id排序
     * @param 商品ID $goods_id
     * @return array 
     */
    public static function findSkuByGoodsId($goods_id) {
        $vars = self::find()->select('*')->where('goods_id=:goods_id AND status = 0',[':goods_id' => $goods_id])->groupBy('spec_id')->all();
        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }
    
    /**
     * 获取规格信息根据goods_id根据spec_value_id排序
     * @param 商品ID $goods_id
     * @return array 
     */
    public static function findSkuByGoodsIdToValue($goods_id) {
        $vars = self::find()->where('goods_id=:goods_id AND status = 0',['goods_id' => $goods_id])->groupBy('spec_value_id')->all();
        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }
    
    public static function findSkuBySpecIdAndSpecValueIdAndGoodsId($spec_id,$spec_value_id,$goods_id) {
        $vars = self::find()
                ->select('*')
                ->where('goods_id=:goods_id AND spec_id=:spec_id AND spec_value_id=:spec_value_id AND status = 0',
                        [':goods_id' => $goods_id, ':spec_id' => $spec_id, ':spec_value_id' => $spec_value_id])
                ->all();

        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }
    
    public static function findProductByGoodsIdAndSpecId($goods_id,$spec_value_id) {
        $vars = self::find()
                ->where('goods_id=:goods_id AND spec_value_id=:spec_value_id AND status = 0',
                        [':goods_id' => $goods_id, ':spec_value_id' => $spec_value_id])
                ->all();

        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }
    
    public static function findProductByGoodsIdAndSpecIdNotMe($goods_id,$spec_value_id,$product_id) {
        $vars = self::find()
                ->leftJoin('d_try.product p','product_spec.product_id = p.product_id')
                ->where('product_spec.goods_id=:goods_id AND product_spec.spec_value_id=:spec_value_id AND product_spec.product_id != :product_id AND product_spec.product_id > 0 AND product_spec.status = 0 AND p.sale_status = 1 AND p.status = 0',
                        [':goods_id'=>$goods_id,':spec_value_id'=>$spec_value_id,':product_id'=>$product_id])
                ->all();

        if(empty($vars)) {
            return [];
        }

        return \yii\helpers\ArrayHelper::toArray($vars);
    }
}
